<?php
/**
 * @author spenceryg
 * @date 2022/11/27
 * @time 12:52 下午
 * @focus love & life
 */


namespace app\auto\model;


use app\auto\business\TableBusiness;
use app\common\lib\mysql\TableModify;
use app\common\lib\mysql\TpTableModify;
use think\Model;

class Field extends \think\Model
{
    protected $table = 'auto_field';

    protected $jsonAssoc = true;

    protected $json = [
        'config'
    ];



    public function iTable(): \think\model\relation\BelongsTo
    {
        return $this->belongsTo(Table::class);
    }

    public static function onAfterDelete(Model $model): void
    {
        // 方法字段删除
        MethodFiled::destroy(function ($query) use($model) {
            $query->where('field_id', $model->getAttr('id'));
        });

        // 表字段sql删除
        $table = Table::find($model->getAttr('table_id'));
        if($table){
            $table_name = $table->getAttr('name');
            $field_name = $model->getAttr('name');
            (new TpTableModify($table_name))->delete($field_name)->execute();
        }

    }
}
